import { querySystemSelect } from "@/api/equip/archive";
import { getFabAreaSelectOptions, queryFabPillarSelectOptions, queryClassList, getFabStatusOptions } from "@/api/modules/fab";
import { ReTag } from "@/components/ReTags";
import { useRouterStoreHook } from "@/store/modules/router";
import { WarnTriangleFilled } from "@px-ui/icons-vue";
import { PxIcon } from "px-ui";

/**
 * @description: 获取search配置信息
 * @param {string} status
 * @param {string} classCode
 * @param {string} area_
 * @param {boolean} isWarning_
 * @return {Array<CommonSearch.SearchOption>}
 */
export const useSearchOption = (status?: string, classCode?: string, area_?: string, isWarning_?: boolean): Array<CommonSearch.SearchOption> => {
  const tabSearchRouterStore = useRouterStoreHook();
  const className: CommonSearch.SearchOption = {
    label: "科室",
    key: "className",
    // value: tabSearchRouterStore.getSubject === "ALL" ? "" : tabSearchRouterStore.getSubject,
    value: "",
    defaultValue: classCode || "",
    type: "select",
    relation: "systemCode",
    relationType: "tree-select",
    props: {
      // clearable: tabSearchRouterStore.getSubject === "ALL"
      clearable: tabSearchRouterStore.getClassList?.length > 1
    },
    // axiosApi: queryClassList,
    option: tabSearchRouterStore.getClassList,
    // mapOption: ({ itemName: label, itemValue: value }) => ({ label, value }),
    mapOption: ({ name: label, code: value }) => ({ label, value }),
    // filterOption: ({ value }) => (tabSearchRouterStore.getSubject === "ALL" ? true : tabSearchRouterStore.getSubject === value),
    relationApi: querySystemSelect,
    getParams: (className: string) => ({ className, factoryArea: "" }),
    relationMapOption: ({ name: label, code: value }) => ({ label, value }),
    placeholder: "请选择科室",
    span: 6
  };

  const systemCode: CommonSearch.SearchOption = {
    label: "系统",
    key: "systemCode",
    value: "",
    type: "tree-select",
    nodeKey: "code",
    treeSelectProps: {
      label: "name",
      children: "children"
    },
    relation: "className",
    relationDefault: true,
    placeholder: "请选择系统",
    span: 6
  };

  const area: CommonSearch.SearchOption = {
    label: "区域",
    key: "area",
    value: "",
    defaultValue: area_ || "",
    type: "select",
    axiosApi: getFabAreaSelectOptions,
    // mapOption: ({ areaId: value = "", areaCode: label = "" }: any) => ({ value, label }),
    formatOption: (res: Array<any>): Array<any> => {
      return res?.map(({ areaId: value = "", areaCode: label = "" }: any) => ({ value, label }))?.filter(({ value, label }) => !!value && !!label);
    },
    placeholder: "请选择区域",
    span: 6
  };

  const columnPlace: CommonSearch.SearchOption = {
    label: "柱位",
    key: "columnPlace",
    value: "",
    type: "select",
    remote: true,
    selectRemoteApi: queryFabPillarSelectOptions,
    getParams: (space: string) => ({ space }),
    mapOption: ({ space = "" }: any) => ({ value: space, label: space }),
    placeholder: "请选择柱位",
    span: 6
  };

  const commonQueryValue: CommonSearch.SearchOption = {
    label: "机台名称/编号",
    key: "commonQueryValue",
    value: "",
    type: "input",
    placeholder: "请输入机台名称/编号",
    span: 6
  };

  const eqpStatus: CommonSearch.SearchOption = {
    label: "状态",
    key: "eqpStatus",
    value: "",
    defaultValue: status || "",
    type: "select",
    axiosApi: getFabStatusOptions,
    mapOption: ({ code: value = "", name: label = "" }: any) => ({ value, label }),
    placeholder: "请选择状态",
    span: 6
  };

  const isWarning: CommonSearch.SearchOption = {
    label: "是否告警",
    key: "isWarning",
    value: null,
    defaultValue: isWarning_ ?? null,
    type: "select",
    option: [
      {
        label: "是",
        value: true
      },
      {
        label: "否",
        value: false
      }
    ],
    placeholder: "请选择是否告警",
    span: 6
  };

  return [className, systemCode, area, commonQueryValue, columnPlace, eqpStatus, isWarning];
};

/**
 * @description: 获取table表格的表头（cloumns）信息
 * @return {*}
 */
export const useTableCloumns = (cb?: (scope: any) => void): Array<CommonTable.TableCloumnsType> => {
  return [
    {
      label: "机台编号",
      prop: "eqpId",
      cloumnClick(scope: any) {
        cb && cb(scope);
      }
    },
    {
      label: "机台名称",
      prop: "eqpName"
    },
    {
      label: "区域",
      prop: "areaId"
    },
    {
      label: "柱位",
      prop: "columnPlace"
    },
    {
      label: "状态",
      prop: "eqpStatus",
      render(scope) {
        const {
          row: { eqpStatus = null }
        } = scope;
        return () => (eqpStatus ? <ReTag size="small" type={scope?.row?.eqpStatus?.toLowerCase()} /> : "--");
      }
    },
    {
      label: "告警",
      prop: "warningTime",
      width: 80,
      render(scope) {
        const {
          row: { warningTime = null }
        } = scope;
        return () =>
          warningTime ? (
            <PxIcon style={{ color: "red", fontSize: "22px" }}>
              <WarnTriangleFilled />
            </PxIcon>
          ) : (
            "无"
          );
      }
    },
    {
      label: "设备工程师",
      prop: "eeOwner"
    },
    {
      label: "值班电话",
      prop: "eePhone"
    },
    {
      label: "工艺工程师",
      prop: "peOwner"
    },
    {
      label: "值班电话",
      prop: "pePhone"
    }
  ];
};
